package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// Activity 活动实体
type Activity struct {
	ent.Schema
}

// Fields 活动字段
func (Activity) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable().
			Comment("活动ID"),
		field.String("name").
			NotEmpty().
			MaxLen(100).
			Comment("活动名称"),
		field.Enum("category").
			Values("team_building", "training", "celebration", "other").
			Comment("活动分类"),
		field.String("category_display").
			NotEmpty().
			MaxLen(50).
			Comment("活动分类中文显示"),
		field.Time("start_time").
			Comment("开始时间"),
		field.Time("end_time").
			Comment("结束时间"),
		field.String("location").
			NotEmpty().
			MaxLen(255).
			Comment("活动地点"),
		field.Text("description").
			Optional().
			Comment("活动描述"),
		field.Bool("is_company_wide").
			Default(true).
			Comment("是否全公司活动"),
		field.Int64("creator_id").
			Comment("创建者ID"),
		field.Bool("notification_app").
			Default(true).
			Comment("APP通知"),
		field.Bool("notification_email").
			Default(true).
			Comment("邮件通知"),
		field.Bool("notification_sms").
			Default(false).
			Comment("短信通知"),
		field.Enum("status").
			Values("draft", "published", "cancelled", "completed").
			Default("draft").
			Comment("活动状态"),
		field.Time("created_at").
			Default(time.Now).
			Immutable(),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now),
	}
}

// Edges 活动关联
func (Activity) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("creator", User.Type).
			Ref("created_activities"). // 指定反向边的名字
			Field("creator_id").
			Unique().
			Required().
			Comment("创建者"),
		edge.To("participants", ActivityParticipant.Type).
			Comment("活动参与者"),
		edge.To("attachments", ActivityAttachment.Type).
			Comment("活动附件"),
		edge.To("reviews", ActReview.Type).
			Comment("活动评论"),
	}
}

// Indexes 索引
func (Activity) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("creator_id"),
		index.Fields("status"),
		index.Fields("category"),
	}
}

// Annotations of the ConversationMember.
func (Activity) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "act_activities",
		},
	}
}
